/**
 * Copyright (C) 2010 Daniel <daniel.petisme@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * 
 */
package fr.daniel.todolist.view.main;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import fr.daniel.todolist.view.model.TodoList;
import fr.daniel.todolist.view.provider.TodoLabelProvider;
import fr.daniel.todolist.view.provider.TodoProvider;
import fr.daniel.todolist.view.sorter.TodoSorter;

/**
 * @author Daniel
 * 
 */
public class TodoListApp extends ApplicationWindow {

	/** Names of the columns */
	private static String[] COLUMNS_TITLES = { "Label", "Priority" };

	/** Bounds of each columns */
	private static int[] COLUMNS_BOUNDS = { 100, 100 };

	/** The table viewier used to display todos */
	private TableViewer viewer;

	/** Provide the content */
	private TodoList model;

	/**
	 * Default Constructor
	 */
	public TodoListApp() {
		super(null);
	}

	/**
	 * Create the table viewer define style and behavior
	 * 
	 * @param parent
	 *            The parent composite
	 */
	private void createViewer(Composite parent) {

		viewer = new TableViewer(parent,SWT.MULTI | SWT.FULL_SELECTION);
		viewer.setContentProvider(new TodoProvider());
		viewer.setLabelProvider(new TodoLabelProvider());
		viewer.setSorter(new TodoSorter());

		createColumns(viewer);
	}

	/**
	 * Create every columns of the TableViewer's table
	 * 
	 * @param table
	 *            the table to make
	 */
	private void createColumns(TableViewer viewier) {

		Table table = viewier.getTable();
		for (int i = 0; i < COLUMNS_TITLES.length; i++) {
			TableColumn column = new TableColumn(table, SWT.BORDER);
			column.setText(COLUMNS_TITLES[i]);
			column.setWidth(COLUMNS_BOUNDS[i]);
			column.setResizable(true);
			column.setMoveable(true);
		}

		table.setHeaderVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets
	 * .Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {

		createViewer(parent);

		model = TodoList.getInstance();
		
		viewer.setInput(model);

		return viewer.getControl();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TodoListApp app = new TodoListApp();
		app.setBlockOnOpen(true);
		app.open();
		Display.getCurrent().dispose();

	}

}
